package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuManagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
//@PreAuthorize("hasRole('ROLE_ADMIN')")
public class MenuManagerController {

    @Reference
    private MenuManagerService menuManagerService;

    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("ddddddddddddddddddddddd");
        PageResult<Menu> result =null;
        try {
            PageInfo<Menu> menuList = menuManagerService.findPage(queryPageBean);
            result = new PageResult<>(menuList.getTotal(),menuList.getList());
            System.out.println(result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取菜单失败");
        }
        return new Result(true, "获取菜单成功",result);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        menuManagerService.add(menu);
        return new Result(true, "添加菜单成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Menu menu){
        menuManagerService.update(menu);
        return new Result(true, "更新菜单成功");
    }
//    deleteById
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
    menuManagerService.deleteById(id);
    return new Result(true, "删除菜单成功");
}

}
